home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / devices and hardware / disks / readsector msdos / readsectormsdos.p < prev   
Encoding:
Text File  |  2000-06-23  |  4.8 KB  |  194 lines

  1. (*
  2.     File:        ReadSectorMSDOS.p
  3.     
  4.     Description:The question was: "I need to write an external file system for floppies
  5.                 formatted like MS-DOS floppies, but with a different directory structure
  6.                 on them."
  7.  
  8.                 The answer was: "Don't even think of writing an external file 
  9.                 system (now)!".
  10.  
  11.                 Instead, convince yourself that reading and writing physical 
  12.                 sectors on MS-DOS floppies is easy, and that you can implement
  13.                 the required functionality within your application (as Apple 
  14.                 File Exchange does).
  15.  
  16.     Author:        JM
  17.  
  18.     Copyright:     Copyright: © 1992-1999 by Apple Computer, Inc.
  19.                 all rights reserved.
  20.     
  21.     Disclaimer:    You may incorporate this sample code into your applications without
  22.                 restriction, though the sample code has been provided "AS IS" and the
  23.                 responsibility for its operation is 100% yours.  However, what you are
  24.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  25.                 after having made changes. If you're going to re-distribute the source,
  26.                 we require that you make it clear in the source that the code was
  27.                 descended from Apple Sample Code, but that you've made changes.
  28.     
  29.     Change History (most recent first):
  30.                 6/24/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  31.                 1/92    Written for THINK Pascal(JM)
  32.  
  33. *)
  34. program ReadSectorMSDOS;  
  35.     uses
  36.         Types,
  37.         Sound,
  38.         ToolUtils,
  39.         Devices;
  40.     const
  41.         cDriveNum = 1;  { internal drive }
  42.         cSectorSize = 512;
  43.         cSectorsPerTrack = 9;  { or 18, for HD disks }
  44.         cSides = 2;  { double-sided only, for now }
  45.  
  46.     var
  47.         gSectBuffer: Ptr;
  48.  
  49.     procedure BailOut (msg: Str255; err: Integer);
  50.     begin
  51.         if err <> 0 then
  52.             Writeln(msg, ' (', err : 6, ')')
  53.         else
  54.             Writeln(msg);
  55.         Writeln('(click mouse to quit)');
  56.         SysBeep(20);
  57.         repeat
  58.         until Button;
  59.         err := Eject(nil, cDriveNum);
  60.         ExitToShell;
  61.     end;
  62.  
  63.     procedure Initialize;
  64.         var
  65.             r: Rect;
  66.     begin
  67.         SetRect(r, 10, 50, 460, 300);
  68.         (*SetTextRect(r);*)
  69.         (*ShowText;*)
  70.         gSectBuffer := NewPtr(cSectorSize);
  71.         if gSectBuffer = nil then
  72.             BailOut('not enough memory ', MemError);
  73.     end;
  74.  
  75.     function FloppyInserted (drive: Integer): Boolean;
  76.         var
  77.             volumeName: Str255;
  78.             vRefNum: Integer;
  79.             freeBytes: Longint;
  80.             err: OsErr;
  81.             evt: EventRecord;
  82.             endRepeat: Boolean;
  83.     begin
  84.         err := Eject(nil, drive);  { make clear something will happen to the drive }
  85.         repeat
  86.             volumeName := '?';
  87.             err := GetVinfo(drive, @volumeName, vRefnum, freeBytes);
  88.             if err = nsvErr then  { no such volume }
  89.                 begin
  90.                     SysBeep(20);
  91.                     Writeln('insert floppy in the internal drive');
  92.                     Writeln('(if you have a SuperDrive , even an MS - DOS one !) ');
  93.                     Writeln('or click the mouse to quit');
  94.                     Writeln;
  95.                     endRepeat := false;
  96.                     repeat
  97.                         if GetNextEvent(everyEvent, evt) then
  98.                             endRepeat := (evt.what in [mouseDown, diskEvt]);
  99.                     until endRepeat;
  100.                     if evt.what = mouseDown then
  101.                         begin
  102.                             err := Eject(nil, drive);  { for convenience, just in case }
  103.                             ExitToShell;
  104.                         end
  105.                     else
  106.                         err := BitShift(evt.message, -16);  { is currently "extFSErr" for MS-DOS floppies }
  107.                 end;
  108.         until err <> nsvErr;
  109.         FloppyInserted := (err <> nsvErr);  { should be more careful here! }
  110.     end;
  111.  
  112.     procedure ReadSector (t, f, n: Integer; p: Ptr);
  113. { read sector <n> on face <f> of track <t> into buffer <p> }
  114.         var
  115.             paramBlock: ParamBlockRec;
  116.             trackSize: Longint;
  117.             err: OsErr;
  118.     begin
  119.         trackSize := cSectorSize * cSectorsPerTrack;
  120.         with paramBlock do
  121.             begin
  122.                 ioCompletion := nil;
  123.                 ioVRefnum := cDriveNum;
  124.                 ioRefnum := -5;
  125.                 ioBuffer := p;
  126.                 ioReqCount := cSectorSize;
  127.                 ioPosMode := fsFromStart;
  128.                 ioPosOffset := trackSize * (cSides * t + f) + cSectorSize * n;
  129.             end;
  130.         err := PBReadSync(@paramBlock);
  131.         if err <> noErr then
  132.             BailOut('PBRead returns ', err);
  133.     end;
  134.  
  135.     procedure DisplaySector (t, f, n: Integer; p: Ptr);
  136.         var
  137.             i: Integer;
  138.             c: Char;
  139.             p0: Ptr;
  140.  
  141.         function Hex (b: SignedByte): Str255;
  142.             var
  143.                 hx: string[16];
  144.                 s: str255;
  145.         begin
  146.             s := 'xx';
  147.             hx := '0123456789ABCDEF';
  148.             s[1] := hx[1 + BitAnd(BitShift(b, -4), $0F)];
  149.             s[2] := hx[1 + BitAnd(b, $0F)];
  150.             Hex := s;
  151.         end;
  152.  
  153.     begin
  154.         p0 := p;
  155.         Writeln('Track ', t : 2, '  -- face ', f : 1, ' -- sector ', n : 2, ' (first 16 bytes):');
  156.         for i := 0 to 15 do
  157.             begin
  158.                 Write(Hex(p^), ' ');
  159.                 p := Ptr(Longint(p) + 1);
  160.             end;
  161.         Write('   ');
  162.         p := p0;
  163.         for i := 0 to 15 do
  164.             begin
  165.                 c := chr(p^);
  166.                 if ord(c) > 31 then
  167.                     Write(c)
  168.                 else
  169.                     Write('.');
  170.                 p := Ptr(Longint(p) + 1);
  171.             end;
  172.         Writeln;
  173.         Writeln;
  174.     end;
  175.  
  176.     procedure DemoTime;
  177.         var
  178.             track: 0..79;
  179.             side: 0..1;
  180.             sector: 0..8;  { cSectorsPerTrack - 1 }
  181.     begin
  182.         track := 0;
  183.         side := 0;
  184.         sector := 0;
  185.         ReadSector(track, side, sector, gSectBuffer);
  186.         DisplaySector(track, side, sector, gSectBuffer);
  187.         BailOut('That''s all folks', 0);
  188.     end;
  189.  
  190. begin
  191.     Initialize;
  192.     if FloppyInserted(cDriveNum) then
  193.         DemoTime;
  194. end.